package com.exartisan.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class ContestPagerAdapter extends PagerAdapter {
	Context context;
//	ArrayList<String> listFileName;
	ArrayList<String> listContestName;
	ArrayList<String> listStartDate;
	ArrayList<String> listEndDate;
	int[] logo;
	LayoutInflater inflater;
	OnClickListener listener;


	public ContestPagerAdapter(Context context,
			ArrayList<String> listContestName, ArrayList<String> listStartDate,
			ArrayList<String> listEndDate, int[] logo, OnClickListener listener) {
		super();
		this.context = context;
		this.listContestName = listContestName;
		this.listStartDate = listStartDate;
		this.listEndDate = listEndDate;
		this.logo = logo;
		this.listener = listener;
	}

	@Override
	public int getCount() {
		return listContestName.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == ((RelativeLayout) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, final int position) {
		// Declare Variables
		TextView tvContest;
		TextView tvStartDate;
		TextView tvEndDate;
		ImageView imgLogo;

		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View itemView = inflater.inflate(com.exartisan.molisasurvey.R.layout.viewpager_item, container,
				false);

		// viewpager_item.xml
		tvContest = (TextView) itemView.findViewById(com.exartisan.molisasurvey.R.id.tvContest);
		tvStartDate = (TextView) itemView.findViewById(com.exartisan.molisasurvey.R.id.tvStartDate);
		tvEndDate = (TextView) itemView.findViewById(com.exartisan.molisasurvey.R.id.tvEndDate);
		imgLogo = (ImageView) itemView.findViewById(com.exartisan.molisasurvey.R.id.imgLogo);

		// Set data
		tvContest.setText(listContestName.get(position));
		tvStartDate.setText("Ngày bắt đầu: "+listStartDate.get(position).substring(0, listStartDate.get(position).indexOf("T")));
		tvEndDate.setText("Hạn nộp bài: "+listEndDate.get(position).substring(0, listEndDate.get(position).indexOf("T")));
		imgLogo.setImageResource(logo[position]);
		itemView.setId(position);
		
		itemView.setOnClickListener(listener);
		
		/*itemView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
//				Intent intent = new Intent(context, SurveyActivity.class);
//				intent.putExtra("data file", listFileName.get(position));
//				context.startActivity(intent);
				onResponse(FakeData.loadQuestionSuccess());
			}

			private void onResponse(String jsonResponse) {
				Intent intent = new Intent(context, SurveyActivity.class);
				intent.putExtra("question data", jsonResponse);
				context.startActivity(intent);
			}
		});*/

		// Add viewpager_item.xml to ViewPager
		((ViewPager) container).addView(itemView);

		return itemView;

	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		// TODO Auto-generated method stub
		((ViewPager) container).removeView((RelativeLayout) object);
	}

}
